package com.example.yls.demoa;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements IOnTimeChangeListener {
    private  Button btn_play;
    private  MyReceiver myReceiver;
    private TextView txt_time;
    private Button btn_stop;

    private Button btnStartIntentService;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myReceiver = new MyReceiver(MainActivity.this);
        IntentFilter filter = new IntentFilter();
        filter.addAction("TIME_CHANGED");
        registerReceiver(myReceiver,filter);

        final Intent intent = new Intent();
        intent.setClass(MainActivity.this,MyService.class);

        txt_time=findViewById(R.id.txt_time);
        btn_play=findViewById(R.id.btn_play);
        btn_stop=findViewById(R.id.btn_stop);
        btnStartIntentService = findViewById(R.id.btn_start_IntentService);
        btn_play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

               startService(intent);
            }

        });
        btn_stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                stopService(intent);
            }
        });
        btnStartIntentService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,MyIntentService.class);
                intent.putExtra("URL","http://www.baiud.com/music.mp3");
                startService(intent);
            }
        });
    }



    @Override
    public void showTime(String str) {
           txt_time.setText(str);
    }
}
